package stream;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/*
收集年龄大于23的人，结果收集到Map中，姓名为键，年龄为值
 */
public class StreamDemo5 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三,23");
        list.add("李四,24");
        list.add("李志鹏,25");
        Map<String, Integer> map = list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                String[] sArr = s.split(",");
                return Integer.parseInt(sArr[1]) > 23;
            }
        }).collect(Collectors.toMap(new Function<String, String>() {
            //名字存键
            @Override
            public String apply(String s) {
                return s.split(",")[0];
            }
        }, new Function<String, Integer>() {
            //年龄存值
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s.split(",")[1]);
            }
        }));
        /*改为lambda表达式
        Map<String, Integer> map = list.stream().filter(s -> {
            String[] sArr = s.split(",");
            return Integer.parseInt(sArr[1]) > 23;
        }).collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1])));*/
        System.out.println(map);
    }
}
